package com.wx.service.impl;

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import com.wx.model.RedisContext;
import com.wx.model.article.ArticleSummaryRecordModel;
import com.wx.service.ArticlemsgService;
import com.wx.service.ToStaticService;
import com.zyht.redis.service.RedisService;
import com.zyht.website.entity.IndexBanner;
import com.zyht.website.service.BannerService;

import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;

@Service
public class ToStaticServiceImpl implements ToStaticService {
	
	@Resource
	private FreeMarkerConfigurer  freeMarkerConfigurer;
	
	@Resource
	private ArticlemsgService articlemsgService;
	
	@Resource
	private BannerService bannerService;
	
	@Resource
	private RedisService redisService;
	
	private String bank_index= "bank/index.html";
	
	private void setCache(String key,String value) {
		redisService.set(String.format(RedisContext.TOSTATIC, key), value);
	}
	
	private String getCache(String key) {
		return redisService.get(String.format(RedisContext.TOSTATIC, key));
	}
	
	private String renderFile(String templateFileName, Map<String, Object> map){
		Configuration configuration = freeMarkerConfigurer.getConfiguration();
		StringWriter sw = new StringWriter();
		try{
			Template template = configuration.getTemplate(templateFileName);
			template.process(map, sw);
		} catch (TemplateException e){
			e.printStackTrace();
		} catch (TemplateNotFoundException e){
			e.printStackTrace();
		} catch (MalformedTemplateNameException e){
			e.printStackTrace();
		} catch (ParseException e){
			e.printStackTrace();
		} catch (IOException e){
			e.printStackTrace();
		}finally{
			try{
				sw.close();
			} catch (IOException e){
				e.printStackTrace();
			}
		}
		return sw.toString();
	}

	@Override
	public boolean staticToIndex(Map<String, Object> map) {
		if(map ==null){
			map = new HashMap<String, Object>();
		}
		
		{//独家新闻
			List< ArticleSummaryRecordModel> list = articlemsgService.getArticleSummary(3L,1,1);
			if(list==null||list.isEmpty()){
			}else{
				map.put("djxw", list);
			}
		}
		{//新户特惠
			List< ArticleSummaryRecordModel> list = articlemsgService.getArticleSummary(4L,1,1);
			if(list==null||list.isEmpty()){
			}else{
				map.put("xhth", list);
			}
		}
		{//企业金融
			List< ArticleSummaryRecordModel> list = articlemsgService.getArticleSummary(5L,1,1);
			if(list==null||list.isEmpty()){
			}else{
				map.put("qyjr", list);
			}
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("valid",1);
		
		List<IndexBanner> bannerList = this.bannerService.list(params);
		map.put("bannerList", bannerList);
		
		String page = renderFile(bank_index,map);
		if(page==null||page.isEmpty()){
			return false;
		}else{
			setCache(bank_index,page);
			return true;
		}
	}

	@Override
	public String getStaticIndex() {
		return getCache(bank_index);
	}
	
}
